#include <QListWidgetItem>
#include <QMessageBox>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "user.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    df = new DataBaseFacade;

    this->IsSingleFile = false;
    this->IsLoggedIn = false;
    this->isDeleting = false;
    this->UserID = 0;

    // This is to show the database login if there is one.
    dbl = new CSDatabaseLogin;
    connect(dbl, SIGNAL(LoggedIn()), this, SLOT(on_LoggedIn()));
    connect(dbl, SIGNAL(NotLoggedIn()), this, SLOT(on_NotLoggedIn()));
    connect(dbl, SIGNAL(Canceled()), this, SLOT(on_Canceled()));
    connect(dbl, SIGNAL(SingleFile()), this, SLOT(on_SingleFile()));
    dbl->exec();
}

MainWindow::~MainWindow()
{
    delete ui;
    delete dbl;
    delete df;
}

void MainWindow::on_btnAdd_clicked()
{
    if (this->IsLoggedIn == true)
    {
        User *u = new User;
        u->FirstName = ui->txtFirstName->text();
        u->LastName = ui->txtLastName->text();
        u->MiddleName = ui->txtMiddleName->text();
        u->UserIsActive = ui->chkUserIsActive->isChecked();
        u->UserName = ui->txtUserName->text();
        u->UserPassword = ui->txtPassword->text();
        df->InsertUser(u);

        this->ReloadList();
        this->UpdateButtons();
    }
}

void MainWindow::on_btnDelete_clicked()
{
    if (this->IsLoggedIn == true)
    {
        this->isDeleting = true;
        User *u = new User;
        u->FirstName = ui->txtFirstName->text();
        u->LastName = ui->txtLastName->text();
        u->MiddleName = ui->txtMiddleName->text();
        u->UserIsActive = ui->chkUserIsActive->isChecked();
        u->UserName = ui->txtUserName->text();
        u->UserPassword = ui->txtPassword->text();
        u->UserID = UserID;
        df->DeleteUser(u);

        this->UserID = 0;
        ui->txtFirstName->setText("");
        ui->txtLastName->setText("");
        ui->txtMiddleName->setText("");
        ui->txtPassword->setText("");
        ui->txtUserName->setText("");
        this->ReloadList();
        this->UpdateButtons();

        this->isDeleting = false;
    }
}

void MainWindow::on_btnUpdate_clicked()
{
    if (this->IsLoggedIn == true)
    {
        User *u = new User;
        u->FirstName = ui->txtFirstName->text();
        u->LastName = ui->txtLastName->text();
        u->MiddleName = ui->txtMiddleName->text();
        u->UserIsActive = ui->chkUserIsActive->isChecked();
        u->UserName = ui->txtUserName->text();
        u->UserPassword = ui->txtPassword->text();
        u->UserID = UserID;
        df->UpdateUser(u);

        this->ReloadList();
        this->UpdateButtons();
    }
}

void MainWindow::on_actionExit_triggered()
{
    this->close();
}

void MainWindow::on_actionAbout_triggered()
{
    QMessageBox::about(this, tr("About Production Estimation Tool"),
    tr("<h2>Production Estimation Tool 1.1</h2>"
    "<p>This was an application I wrote several years back and</p>"
    "<p>I thought it was a good idea to port it to Qt and C++.</p>"
    "<p>This is just something to help promoters and production professionals estimate costs.</p>"
    "<p>You can access the source code and other documentation <a href='http://entertools.codeplex.com'>here.</a></p>"));
}

void MainWindow::on_Canceled()
{
    this->DisconnectLoginDialog();
}

void MainWindow::on_LoggedIn()
{
    this->DisconnectLoginDialog();
    this->IsLoggedIn = true;
    this->ReloadList();
    this->UpdateButtons();
}

void MainWindow::on_NotLoggedIn()
{
    this->DisconnectLoginDialog();
}

void MainWindow::on_SingleFile()
{
    this->DisconnectLoginDialog();
}

void MainWindow::DisconnectLoginDialog()
{
    this->dbl->close();
    disconnect(dbl, SIGNAL(LoggedIn()), this, SLOT(on_LoggedIn()));
    disconnect(dbl, SIGNAL(NotLoggedIn()), this, SLOT(on_NotLoggedIn()));
    disconnect(dbl, SIGNAL(Canceled()), this, SLOT(on_Canceled()));
    disconnect(dbl, SIGNAL(SingleFile()), this, SLOT(on_SingleFile()));
}

void MainWindow::on_lstUsers_itemClicked(QListWidgetItem *item)
{
    if (this->isDeleting == false && this->IsLoggedIn == true)
    {
        this->UserID = item->data(Qt::UserRole).toInt();
        User *u = new User;
        df->GetUserByUserID(this->UserID,u);
        ui->txtFirstName->setText(u->FirstName);
        ui->txtLastName->setText(u->LastName);
        ui->txtMiddleName->setText(u->MiddleName);
        ui->txtPassword->setText(u->UserPassword);
        ui->txtUserName->setText(u->UserName);
        ui->chkUserIsActive->setChecked(u->UserIsActive);
        delete u;

        this->UpdateButtons();
    }
}

void MainWindow::UpdateButtons()
{
    if (this->isDeleting == false && this->IsLoggedIn == true)
    {
        if (this->UserID > 0)
        {
            ui->btnDelete->setEnabled(true);
            ui->btnUpdate->setEnabled(true);
        }
        else
        {
            ui->btnDelete->setEnabled(false);
            ui->btnUpdate->setEnabled(false);
        }
    }
}

void MainWindow::ReloadList()
{
    this->isDeleting = true;
    ui->lstUsers->clear();
    df->GetAllUsers();
    for(int i = 0; i < df->Users->count(); i++)
    {
        QListWidgetItem *item = new QListWidgetItem(df->Users->at(i)->UserName, ui->lstUsers);
        item->setData(Qt::UserRole, df->Users->at(i)->UserID);
    }
    this->isDeleting = false;
}

void MainWindow::on_btnClear_clicked()
{
    this->UserID = 0;
    ui->txtFirstName->setText("");
    ui->txtLastName->setText("");
    ui->txtMiddleName->setText("");
    ui->txtPassword->setText("");
    ui->txtUserName->setText("");
    this->ReloadList();
    this->UpdateButtons();
}
